package com.whksoft.mymapplugin.API;

import com.whksoft.mymapplugin.MyMapPlugin;
import com.whksoft.mymapplugin.NMS.*;
import com.whksoft.mymapplugin.Renderer.ImageRenderer;
import com.whksoft.mymapplugin.Renderer.QRRenderer;
import com.whksoft.mymapplugin.utils.ImageUtils;
import com.whksoft.mymapplugin.utils.itemUtils;
import com.whksoft.mymapplugin.utils.versionUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.tags.ItemTagType;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import org.bukkit.persistence.PersistentDataType;

public class ZMapManager {

    //创建扫码地图
    private static ItemStack createItem(ZMap zMap, Player p){
        ItemStack itemStack;
        MapView view = Bukkit.createMap(p.getWorld());
        //清除渲染器
        for (MapRenderer mapRenderer : view.getRenderers()) {
            if(mapRenderer == null) break;
            view.removeRenderer(mapRenderer);
        }

        if(zMap.getStr() != null) view.addRenderer(new QRRenderer(zMap.getStr(), MyMapPlugin.getInstance()));
        else {
            if(!(zMap.getImage().getHeight()==128&&zMap.getImage().getWidth()==128))
                view.addRenderer(new ImageRenderer(ImageUtils.resize(zMap.getImage(),128,128), MyMapPlugin.getInstance()));
            else view.addRenderer(new ImageRenderer(zMap.getImage(), MyMapPlugin.getInstance()));
        }

        itemStack = new ItemStack(Material.FILLED_MAP, 1);

        // for 1.13+ 设置地图ID
        if(versionUtils.getMiddleVersion() >= 13){
            switch (versionUtils.getNMSVersion()) {
                case "1_13_R2":
                    itemStack = new NBT1_13_R2(itemStack).setMapID(view.getId());
                    break;
                case "1_14_R1":
                    itemStack = new NBT1_14_R1(itemStack).setMapID(view.getId());
                    break;
                case "1_15_R1":
                    itemStack = new NBT1_15_R1(itemStack).setMapID(view.getId());
                    break;
                case "1_16_R1":
                    itemStack = new NBT1_16_R1(itemStack).setMapID(view.getId());
                    break;
                case "1_16_R2":
                    itemStack = new NBT1_16_R2(itemStack).setMapID(view.getId());
                    break;
                case "1_16_R3":
                    itemStack = new NBT1_16_R3(itemStack).setMapID(view.getId());
                    break;
            }
        }else{
            itemStack.setDurability((short) view.getId());
        }

        ItemMeta meta = itemStack.getItemMeta();
        NamespacedKey key = new NamespacedKey(MyMapPlugin.getInstance(), "isQRCode");
        assert meta != null;

        if(versionUtils.getMiddleVersion() >=13){
            if(versionUtils.getMiddleVersion() != 13)
                meta.getPersistentDataContainer().set(key, PersistentDataType.INTEGER, 1);
            else
                meta.getCustomTagContainer().setCustomTag(key, ItemTagType.INTEGER, 1);
        }else{
            String QRKey = "isQRCode";
            switch (versionUtils.getNMSVersion()) {
                case "1_12_R2":
                    itemStack = new NBT1_12_R1(itemStack).setDataContainer(QRKey);
            }
        }

        meta.setDisplayName(zMap.getName());
        meta.setLore(zMap.getLore());
        if(zMap.isHideEnchantment()){
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            meta.addEnchant(zMap.getEnchantment(),1,true);
        }

        itemStack.setItemMeta(meta);
        return itemStack;
    }

    /**
     * 给予一个玩家扫码地图
     * @param zmap ZMap对象
     * @param player 玩家
     */
    public static void give(ZMap zmap, Player player){
        ItemStack renderedMap = createItem(zmap, player);

        boolean hotBar = false;
        int emptyBar = 0;
        for (int i = 0; i < 9; i++) {
            ItemStack item = player.getInventory().getItem(i);
            if (item == null) {
                hotBar = true;
                emptyBar = i;
            }
        }

        if (hotBar) {
            if (player.getInventory().getItemInMainHand().getType() == Material.AIR) //主手为空
            {
                player.getInventory().setItem(player.getInventory().getHeldItemSlot(), renderedMap);
            } else {
                player.getInventory().setItem(emptyBar, renderedMap);
                player.getInventory().setHeldItemSlot(emptyBar);
            }
        } else {
            int empty = 0;              //背包里空栏位的索引值
            for (int i = 9; i < 36; i++) {
                ItemStack item = player.getInventory().getItem(i);
                if (item == null) {
                    empty = i;
                    break;
                }
            }
            //主手
            ItemStack mainHand = player.getInventory().getItemInMainHand();
            player.getInventory().setItem(empty,mainHand);
            mainHand.setAmount(0);
            player.getInventory().setItem(player.getInventory().getHeldItemSlot(),renderedMap);
        }
    }

    /**
     * 清除一个玩家手持的扫码地图
     * @param p 玩家
     */
    public static void clear(Player p) {
        itemUtils.clearMaps(p);
    }
}
